package org.jks.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jks.model.FlightAngle;
import org.jks.response.model.ActionResult;
import org.jks.response.model.UITable;
import org.jks.service.FlightAngleService;
import org.jks.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/flight_angle")
public class FlightAngleController {
	@Autowired
	FlightAngleService flightAngleService;
	
	@RequestMapping("/{id}")
	public void all(@PathVariable long id, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		String[] limit = request.getParameterValues("limit");
		String[] pages = request.getParameterValues("page");
		int pagesize = 25;
		int page = 1;
		if(limit.length > 0){
			pagesize = Integer.valueOf(limit[0]);
		}
		if(pages.length > 0){
			page = Integer.valueOf(pages[0]);
		}
		try{
			long total = flightAngleService.countByFlightLineId(id);
			List<FlightAngle> flightAngles = flightAngleService.findByFlightLineId(id, page, pagesize);
			for(FlightAngle flightAngle : flightAngles){
				flightAngle.setFlightLine(null);
			}
			ar.setSuccess(true);
			UITable table = new UITable();
			table.setTotal(total);
			table.setRows(flightAngles);
			ar.setData(table);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/searchFlightAngle")
	public void searchFlightAngle(FlightAngle fa, HttpServletRequest request, HttpServletResponse response)
	{
		ActionResult ar = new ActionResult();
		try{			
			List<FlightAngle> flightAngles = flightAngleService.searchFlightAngle(fa);
			for(FlightAngle flightAngle : flightAngles)
			{
				flightAngle.setFlightLine(null);
			}
			ar.setSuccess(true);
			UITable table = new UITable();
			table.setRows(flightAngles);
			ar.setData(table);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
}
